//
//  PhotoAnnotationView.m
//  XPhoto
//
//  Created by baboy on 2019/7/11.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "BKAnnotationView.h"
#import "BKHttpRequestManager.h"
#import "UIImage+x.h"


// Drawing code
#define SHADOW_COLOR [UIColor lightGrayColor]
#define SHADOW_OFFSET CGSizeMake(1, 1)
#define BACKGROUND_RADIUS 5
#define BACKGROUND_COLOR [UIColor whiteColor]
#define CONTENT_PADDING 2
#define CONTENT_MARGIN 2
#define ANCHOR_HEIGHT 5
#define CONTENT_WIDTH 50

@implementation BKAnnotationView
- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.bounds = CGRectMake(0, 0, CONTENT_WIDTH, CONTENT_WIDTH+ANCHOR_HEIGHT);
        self.opaque = NO;
        _backgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
        
        CGRect imageViewFrame = CGRectInset([self contentRect], CONTENT_PADDING, CONTENT_PADDING);
        _imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
        _imageView.layer.cornerRadius = BACKGROUND_RADIUS;
        _imageView.clipsToBounds = YES;
        [self addSubview:_imageView];
        
//        [_backgroundView addSubview:_imageView];
//        _backgroundView.image = [UIImage imageNamed:@"image"];
//        [self addSubview:_backgroundView];
        
        
//        [self sendSubviewToBack:_backgroundView];
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
//        [imageView setImage:[UIImage imageNamed:@"image"]];
//        imageView.backgroundColor = [UIColor redColor];
//        [self addSubview:imageView];
    }
    return self;
}
- (void)setImageURLString:(NSString *)url{
    [[BKHttpRequestManager defaultManager] download:url
                                           progress:^(NSProgress * _Nullable downloadProgress) {
                                               
                                           }
                                            success:^(id  _Nonnull task, id  _Nullable url) {
                                                if (url) {
                                                    UIImage *image = [UIImage imageWithContentsOfFile:[url path]];
                                                    self.imageView.image = image;
                                                }
                                           }
                                            failure:^(id  _Nullable task, id  _Nullable fp, NSError * _Nonnull error) {
                                                
                                            }];
}
- (CGRect) contentRect{
    CGRect contentRect = CGRectInset(self.bounds, CONTENT_MARGIN, CONTENT_MARGIN);
    contentRect.size.height -= ANCHOR_HEIGHT;
    return contentRect;
}
- (CGPoint)centerOffset{
    CGPoint p = CGPointMake(0, -self.bounds.size.height/2);
    return p;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    float blur = 1.0;
    CGSize imageSize = self.bounds.size;
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    
//    [[UIColor clearColor] set];
    CGRect contentRect = [self contentRect];
    CGContextSaveGState(ctx);
    if (SHADOW_COLOR) {
        CGContextSetShadowWithColor(ctx, SHADOW_OFFSET, blur, [SHADOW_COLOR CGColor]);
    }
    //背景轮廓
    CGMutablePathRef path = CGPathCreateMutable();
    float minx = CGRectGetMinX(contentRect), midx = CGRectGetMidX(contentRect), maxx = CGRectGetMaxX(contentRect);
    float miny = CGRectGetMinY(contentRect), midy = CGRectGetMidY(contentRect), maxy = CGRectGetMaxY(contentRect);
    
    CGPathMoveToPoint(path,NULL, minx, midy);
    CGPathAddArcToPoint(path,NULL, minx, miny, midx, miny, BACKGROUND_RADIUS);
    CGPathAddArcToPoint(path,NULL, maxx, miny, maxx, midy, BACKGROUND_RADIUS);
    CGPathAddArcToPoint(path,NULL, maxx, maxy, midx+ANCHOR_HEIGHT/2, maxy, BACKGROUND_RADIUS);
    CGPathAddLineToPoint(path, NULL, midx+ANCHOR_HEIGHT/2, maxy);
    CGPathAddLineToPoint(path, NULL, midx, maxy+ANCHOR_HEIGHT);
    CGPathAddLineToPoint(path, NULL, midx-ANCHOR_HEIGHT/2, maxy);
    CGPathAddArcToPoint(path,NULL, minx, maxy, minx, midy, BACKGROUND_RADIUS);
    
    
    
    CGContextBeginPath(ctx);
    CGContextAddPath(ctx, path);
    CGContextClosePath(ctx);
    CGContextSetFillColorWithColor(ctx,[BACKGROUND_COLOR CGColor]);
    CGContextFillPath(ctx);
    CGPathRelease(path);
    
//    CGContextSetShadowWithColor(ctx, CGSizeZero, 0, NULL);
//
//    path = createRoundCornerPath(contentRect, BACKGROUND_RADIUS);
//    CGContextBeginPath(ctx);
//    CGContextAddPath(ctx, path);
//    CGContextClosePath(ctx);
//    CGContextClip(ctx);
//    CGPathRelease(path);
}

@end
